package cn.com.controler.User;

import cn.com.entity.User;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class UserDAO {

    //添加数据
    public int adduser(User user){
        //处理时间
        Date srdate = String_date(user.getUserBirthday());
        Date zcdate = new Date();
        String str = "insert into user values(null,?,?,?,?,?,?,0)";
        Object[]arr = new Object[]{user.getUsername(),user.getUserPassword(),srdate,user.getUserTel(),user.getUserEmail(),zcdate};
        return UserDAOTemplete.insertdata(str, arr);

    }
    //按照ID删除数据
    public int deletebyid(Integer id){
        String str = "delete from user where UserId = ?";
        Object[] arr = new Object[]{id};
        return UserDAOTemplete.insertdata(str,arr);
    }
    //修改数据,修改用户名，生日，联系方式，邮件
    public int updateuser(String uname,String ubtime,String utel,String uemail,Integer id){
        String str = "update user set Username = ?,UserBirthday = ?,UserTel = ?,UserEmail=? where UserId = ?";
        Object[] arr = new Object[]{uname,ubtime,utel,uemail,id};
        return UserDAOTemplete.insertdata(str,arr);
    }
    //修改密码
    public int updatepass(String pass,Integer id){
        String str = "update user set UserPassword = ? where UserId = ?";
        Object []arr = new Object[]{pass,id};
        return UserDAOTemplete.insertdata(str,arr);
    }
    //根据用户名密码查询数据
    public List upselect(String username,String password){
        String str = " select *from user where Username = ? and UserPassword = ?";
        Object[] arr = new Object[]{username,password,};
        return UserDAOTemplete.selectdata(str, arr);

    }
    //根据用户名密码查询数据
    public List upselectbyuname(String username){
        String str = "select *from user where Username = ?";
        Object[] arr = new Object[]{username};
        return UserDAOTemplete.selectdata(str, arr);

    }
    //使用主键查询
    public List idselect(Integer id){
        String str = "select *from user where UserId = ?";
        Object[] arr = new Object[]{id};
        return UserDAOTemplete.selectdata(str,arr);
    }
    //使用用户名，邮箱，电话号查询数据
    public List uetselect(String username,String email,String tel){
        String str = "select *from user where Username = ? and UserEmail = ? and UserTel = ?";
        Object []arr = new Object[]{username,email,tel};
        return  UserDAOTemplete.selectdata(str,arr);
    }
    //查询所有用户，按照用户类型降序排列
    public List<User> selectalluser(){
        String str = "select *from user order by UserRoleType desc";
        Object []arr = new Object[]{};
        return  UserDAOTemplete.selectdata(str,arr);
    }
    //根据id修改用户为vip的状态，由于没有指定外检，管理员处理申请
    public int updatestateagree(int id){
        String str = "update user set UserRoleType = 1 where UserId = ?";
        Object []arr = new Object[]{id};
        return UserDAOTemplete.insertdata(str,arr);
    }
    //根据id拒绝vip的状态
    public int updatestaterefuse(int id){
        String str = "update user set UserRoleType = 2 where UserId = ?";
        Object []arr = new Object[]{id};
        return UserDAOTemplete.insertdata(str,arr);
    }








    //时间处理
    public String Date_string(Date date){
        SimpleDateFormat sa = new SimpleDateFormat("yyyy-MM-dd");
        return sa.format(date);
    }
    public Date String_date(String string){
        SimpleDateFormat sa = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return sa.parse(string);
        } catch (ParseException e) {
            e.printStackTrace();
        }
       return null;
    }
}
